<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .register-box {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }
        .register-box form {
            display: flex;
            flex-direction: column;
        }
        .register-box .form-group {
            display: flex;
            margin-bottom: 20px;
        }
        .register-box .form-group label {
            flex: 0 0 100px; /* 固定标签的宽度 */
            margin-right: 10px;
        }
        .register-box .form-group input[type="text"],
        .register-box .form-group input[type="password"] {
            flex: 1; /* 输入框占据剩余空间 */
            width: 1%; /* 为了让 flex 布局生效，设置一个小宽度 */
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        .register-box input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .register-box input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="register-box">
        <form id="registerForm">
            <div class="form-group">
                <label for="username">用户名：</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码：</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="confirm-password">确认密码：</label>
                <input type="password" id="confirm-password" name="confirm-password" required>
            </div>
            <input type="submit" value="注册">
        </form>
    </div>

    <script>
        document.getElementById('registerForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm-password').value;

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致！');
                return;
            }

            fetch('/users/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 0) {
                    alert('注册成功！');
                    // 可以在这里添加代码重定向到登录页面或其他操作
                } else {
                    alert('注册失败：' + data.msg);
                }
            })
            .catch(error => {
                console.error('注册请求失败:', error);
                alert('注册请求失败，请稍后重试。');
            });
        });
    </script>
</body>
</html>